package com.xiaoxi.bms.common.manager;

import com.xiaoxi.bms.common.provider.BMSSecurityAuthenticationProvider;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName BMSSecurityAuthenticationManager
 * @Description 自定义 SpringSecurity 认证管理器
 * @Author xiaoxi
 * @Date 2022/4/24 14:39
 * @Version 1.0
 */
@Component
public class BMSSecurityAuthenticationManager implements AuthenticationManager {

    @Resource
    private BMSSecurityAuthenticationProvider bmsSecurityAuthenticationProvider;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        Authentication result = bmsSecurityAuthenticationProvider.authenticate(authentication);

        if (Objects.nonNull(result)) {
            return result;
        }

        throw new ProviderNotFoundException("Authentication failed!");

    }
}
